#!/usr/bin/env python
# -*- coding: utf-8 -*-

# @Time     :2020/10/21
# @Author   :Changshu
# @File     :Exercise_371.py
# 371. 两整数之和
# 不使用运算符 + 和 - ​​​​​​​，计算两整数 ​​​​​​​a 、b ​​​​​​​之和。

'''太难想了'''
def getSum(a: int, b: int) -> int:
	# 2^32
	MASK = 0x100000000
	# 整型最大值
	MAX_INT = 0x7FFFFFFF
	MIN_INT = MAX_INT + 1
	while b != 0:
		# 计算进位
		carry = (a & b) << 1
		# 取余范围限制在 [0, 2^32-1] 范围内
		a = (a ^ b) % MASK
		b = carry % MASK
	return a if a <= MAX_INT else ~((a % MIN_INT) ^ MAX_INT)

if __name__ == '__main__':
	a=-12
	b=-18
	print(getSum(a,b))